from selenium import webdriver
from selenium.webdriver.common.by import By
class hospital:
    def __init__(self,driver):
        self.driver = driver
    def dakai(self,url):
        self.driver.get(url)
        self.driver.implicitly_wait(15)
    def shousuo(self):
        self.driver.find_element(By.NAME,'q').send_keys('骨科')
    def dianji(self):
        self.driver.find_elements(By.CLASS_NAME,'btn')[0].click()
    def qiehuan(self):
        wins = self.driver.window_handles
        self.driver.switch_to.window(wins[-1])

    def lianjie(self):
        self.driver.find_element(By.XPATH, "/html/body/div[4]/ul/li[1]/div[1]/h5/a").click()
        #self.driver.switch.find_element(By.XPATH,'/html/body/div[4]/ul/li[1]/div[1]/h5/a').click
    def duanyan(self):
        self.driver.switch_to.window(driver.window_handles[-1])
        text1 = self.driver.find_element(By.XPATH,"/html/body/div[3]/div/div/article/div[2]/p[2]/span[1]").text
        print(text1)
        assert "基本概况" in text1
if __name__ == '__main__':
    option = webdriver.ChromeOptions()  # todo：chrome浏览器配置项
    option.add_experimental_option("detach", True)  # todo：浏览器不会自动关闭
    driver = webdriver.Chrome(options=option)
    a = hospital(driver)
    a.dakai("http://www.bjhospital.net/")
    a.shousuo()
    a.dianji()
    a.qiehuan()
    a.lianjie()
    a.duanyan()

"""
option = webdriver.ChromeOptions() # todo：chrome浏览器配置项
option.add_experimental_option("detach", True)# todo：浏览器不会自动关闭
driver = webdriver.Chrome(options=option)
driver.get('http://www.bjhospital.net/')
driver.implicitly_wait(15)
driver.find_element(By.NAME,'q').send_keys('骨科')
driver.find_element(By.XPATH,'//*[@id="header"]/div/div/div/form/button').click()
# driver.find_elements(By.CLASS_NAME,"btn")[0].click()
wins = driver.window_handles
print(wins)
driver.switch_to.window(wins[-1])
driver.find_element()
"""
